function resize_result()
{
	result_width = size['width'] - $('result').getStripStyle('left') - $('result').getStripStyle('margin-left') - $('result').getStripStyle('margin-right') - $('result').getStripStyle('padding-left') - $('result').getStripStyle('padding-right');
	
	result_height = size['height'] - $('result').getStripStyle('top') - $('result').getStripStyle('margin-top') - $('result').getStripStyle('margin-bottom') - $('result').getStripStyle('padding-top') - $('result').getStripStyle('padding-bottom');
	
	result_toolbar_height = $('result_toolbar').getStripStyle('height');
	
	$('result').setStyle({'width': (result_width)+'px'});	
	$('result').setStyle({'height': (result_height)+'px'});
	
	$('result_area').setStyle({'width': (result_width)+'px'});	
	$('result_area').setStyle({'height': (result_height - result_toolbar_height)+'px'});		
}

function resize_column(e,width)
{
	if(width < 0)
	{
		width = 20;
		if(e !=0)
			var izquierda = Number($('slide_'+(Number(e)-1)).getStripStyle('left')) + Number(width) + 1;
		else
			var izquierda = width;
		//alert($('slide_'+i).getStyle('left'));
		$('slide_'+e).setStyle({'left': izquierda+'px'});		
	}
	cant = $('result_table').childElements();
	tam = cant[1].childElements();
	var tem = tam.size();
	tar = cant[0].childElements();
	cols = tar[0].childElements();
	var inital_width = cols[e].getStripStyle('width');
	cols[e].firstDescendant().setStyle({'width': (width-10)+'px'});	
	cols[e].setStyle({'width': width+'px'});
	var number_columns = cols.size();
	var width_difference = width - inital_width;
	for(i = (e+1); i <  number_columns; i++)
	{
		var izquierda = Number($('slide_'+i).getStripStyle('left')) + Number(width_difference);
		//alert($('slide_'+i).getStyle('left'));
		$('slide_'+i).setStyle({'left': izquierda+'px'});
	}
	for(i = 0; i <  tem; i++)
	{
		cols = tam[i].childElements();
		cols[e].firstDescendant().setStyle({'width': (width-10)+'px'});	
		cols[e].setStyle({'width': width+'px'});	
	}
	
}


var Result = {
	isEditable : false,
	values : Array(),
	cw_value: '',
	cw_cell: '',
	
	setEditable: function()
	{
		if(this.isEditable)
			this.isEditable = false;
		else
			this.isEditable = true;
	},
	
	edit : function(e)
	{
		if(this.isEditable)
		{
			height = $(''+e.id).getStyle('height');
			width = $(''+e.id).getStyle('width')

			this.cw_value = $(e).firstDescendant().innerHTML;
			this.cw_cell = e.id;
			
			e.innerHTML = '<input type="text" id="cell_editor" value="' + this.cw_value + '" />';
		
		
			$('cell_editor').setStyle({'height': height});	
			$('cell_editor').setStyle({'width': width});

			document.onmousedown=function(e)
					{
						if(e)
							event = e;
						var element = Event.element(event);
						Result.close_editor(element);
					};
			document.onkeydown=function(e)
					{
						if(e)
							event = e;				
						switch(event.keyCode)
						{	
							case 9:
								alert("Tab");
							break;
							case 13:
								alert("Enter");
							break;
						}
					};
			$('cell_editor').focus();

		}
	},
	
	close_editor: function(element)
	{
		if(element.id != "cell_editor")
		{
			row = this.cw_cell.substr(5,(this.cw_cell.lastIndexOf('_')-this.cw_cell.indexOf('_')-1));
			col = this.cw_cell.substr(this.cw_cell.lastIndexOf('_')+1);
			
			if(!is_array(this.values['' + row]))
			{
				this.values['' + row] = new Array();
				this.values['' + row]['' + col] = {'new_value': $F('cell_editor'), 'old_value':this.cw_value};	
			}
			else
			{
				this.values['' + row]['' + col] = {'new_value': $F('cell_editor'), 'old_value':this.cw_value};		
			}
			
			$(this.cw_cell).innerHTML = '<div>' + $F('cell_editor') + '</div>';
			Result.update();
			
			document.onmousedown="";
			document.onkeydown=""; 
		//	$('row_' + row).addClassName('result_modified_row');
		}	
	},
	
	update: function()
	{

		row = this.cw_cell.substr(5,(this.cw_cell.lastIndexOf('_')-this.cw_cell.indexOf('_')-1));
		col = this.cw_cell.substr(this.cw_cell.lastIndexOf('_')+1);
		
		query_str = "UPDATE " + $('col_table_'+col).value + " SET `" + $('col_name_'+col).value + "` = '" +  $(this.cw_cell).firstDescendant().innerHTML +"'" ;
		cant = $('result_table').childElements();
		tam = cant[1].childElements();
		var tem = tam.size();
		tar = cant[0].childElements();
		cols = tar[0].childElements();
		var number_columns = cols.size();
	//	var width_difference = width - inital_width;
		query_str += " WHERE "

		for(i = 0; i <  number_columns; i++)
		{
			if($('col_primary_key_'+i).value > 0)
			{
				query_str += $('col_name_'+i).value + "=";
				
				if(col == i)
					query_str += this.cw_value;
				else
					query_str += $('cell_'+row+'_'+i).firstDescendant().innerHTML;
			}
		}	
		
		if(confirm(query_str + " ? "))
		{
			execute(query_str);
		}
/*	for(i=0; i< cw_updates.length; i++)
	{
		if(is_array(cw_updates['' + i]))
		{
			str = "";
			for(e=0; e< cw_updates['' + i].length; e++)
			{
				if(cw_updates['' + i]['' + e])
				{
					str += cw_updates['' + i]['' + e].old_value+ ": "+cw_updates['' + i]['' + e]['new_value']+", ";
				}
			}
			alert(str);
		}
	}*/	
	}
};
